/**
 * 查看wiki行为统计
 * @param sign
 * @param userId
 * @param time
 * @param ip
 */
var wikiViewBhv = function (sign, userId, time, ip,login_type) {

    // 获取当前url
    var url = document.location.toString();
    // 如果url后面带有 get参数 则只提取 ? 前的url
    if(url.indexOf('?') !== -1){
        url = url.split('?')[0];
    }
    // 如果url后面带有 get参数 则只提取 # 前的url
    if(url.indexOf('#') !== -1){
        url = url.split('#')[0];
    }
    // 获取百科的key
    var key = url.split('/').pop();

    var data = {
        ip: ip,
        time: time,
        sign: sign,
        data: {
	    "login_type": login_type,//solid - 有登录用户
            "plates": "wiki",
            "action": "behavior",
            "bhv_type": "view",
            "user_id": userId,
            "act_obj": key,
            "obj_type": "item"
        }
    };

    uploadBhvLog(data);
};

var docViewTuijian = function(params){

	var tuijiang_item = new Object;
	tuijiang_item.itemId = params.key;
	tuijiang_item.title = params.title;
	tuijiang_item.content = params.content;
	tuijiang_item.url = params.url;
	tuijiang_item.categoryId = params.category;
	tuijiang_item.publishTime = params.publishTime;
	tuijiang_item.tag = params.tags;


	tuijiang_item = JSON.stringify(tuijiang_item);

	var data = {
        type: "item",
        item: tuijiang_item
	};
	uploadTuijianLog(data);
}

var docRelateViewTuijian = function(params){
	var tuijiang_item = JSON.stringify(params);
	var data = {
        type: "bhv",
        bhv: tuijiang_item
	};
	uploadTuijianLog(data);
}

var docClickTuijian = function(params){

	var tuijianxitong_bhv = '';

	var timestamp = (new Date()).getTime();
	var tuijiang_item = new Object;
	tuijiang_item.itemId = params.key;
	tuijiang_item.actionTime = timestamp;
	tuijiang_item.action = 'detailPageShow';
	tuijiang_item.itemSetId = params.itemSetId;
	tuijiang_item.sceneId = params.sceneId;
	tuijiang_item.userId = params.userId;

	var tuijianxitong_bhv = new Array();

	tuijianxitong_bhv.push(tuijiang_item);
	tuijianxitong_bhv = JSON.stringify(tuijianxitong_bhv);

	var data = {
        type: "bhv",
        bhv: tuijianxitong_bhv
	};
	uploadTuijianLog(data);
}

/**
 * 查看文档详情页统计
 * @param sign
 * @param userId
 * @param time
 * @param ip
 */
var docViewBhv = function (params) {

    var sign = params.sign;
    var userId = params.userId;
    var time = params.time;
    var ip = params.ip;
    var login_type = params.login_type;
    // 获取当前url
    var url = document.location.toString();
    // 如果url后面带有 get参数 则只提取 ? 前的url
    if(url.indexOf('?') !== -1){
        url = url.split('?')[0];
    }
    // 获取文档的key
    url = url.split('/').pop();
    var key = url.split('.')[0];
	    

    var data = {
        ip: ip,
        time: time,
        sign: sign,
        data: {
	    "login_type": login_type,//solid - 有登录用户
            "plates": "doc",
            "action": "behavior",
            "bhv_type": "view",
            "user_id": userId,
            "act_obj": key,
            "obj_type": "item"
        }
    };

    uploadBhvLog(data);
};

/**
 * 相关文档点击统计
 *
 */
var docClickBhv = function(params){
	var sign = params.sign;
	var userId = params.userId;
	var time = params.time;
	var ip = params.ip;
	var login_type = params.login_type;
	var key = params.key;
	var data = {
        ip: ip,
        time: time,
        sign: sign,
        data: {
	    "login_type": login_type,//solid - 有登录用户
            "plates": "doc",
            "action": "behavior",
            "bhv_type": "detail_click",
            "user_id": userId,
            "act_obj": key,
            "obj_type": "item"
        }
    };
	uploadBhvLog(data);
}

/**
 * 相关文档展示统计
 *
 */
var docRelateViewBhv = function(params){
	var sign = params.sign;
	var userId = params.userId;
	var time = params.time;
	var ip = params.ip;
	var login_type = params.login_type;

	var key = params.key;
	var data = {
        ip: ip,
        time: time,
        sign: sign,
        data: {
	    "login_type": login_type,//solid - 有登录用户
            "plates": "doc",
            "action": "behavior",
            "bhv_type": "detail_show",
            "user_id": userId,
            "act_obj": key,
            "obj_type": "item"
	      }
	};
	uploadBhvLog(data);
}

/**
 * 查看资讯详情页统计
 * @param sign
 * @param userId
 * @param time
 * @param ip
 */
var newsViewBhv = function (sign, userId, time, ip,login_type) {

    // 获取当前url
    var url = document.location.toString();
    // 如果url后面带有 get参数 则只提取 ? 前的url
    if(url.indexOf('?') !== -1){
        url = url.split('?')[0];
    }
    // 获取资讯的ID
    url = url.split('/').pop();
    var key = url.split('.')[0];

    var data = {
        ip: ip,
        time: time,
        sign: sign,
        data: {
	    "login_type": login_type,//solid - 有登录用户
            "plates": "news",
            "action": "behavior",
            "bhv_type": "view",
            "user_id": userId,
            "act_obj": key,
            "obj_type": "item",
        }
    };

    uploadBhvLog(data);
};

/**
 * 日志埋点
 * @param params {ip: string, time: *, sign: *, data: {除公共参数外的其他参数}}
 */
var uploadBhvLog = function (params) {
    var domain = document.domain;

    var patt = /\.(test|dev)\./i;
    var dev = domain.search(patt);
    // 根据域名获取发送日志地址
    if(dev > 0)
    {
	    //var url = 'http://192.168.1.158:7201';
	    var url = 'http://192.168.1.130:7201';
    }else
    {
	    var url = 'https://log.mbalib.com';
    }
    return false;

    /**
     *
     * @type {{env: {ip: (*|string)}, time: *, "User-Agent": string, sign: *}}
     */
    var data = {
        //env: {ip: params.ip},
        time: params.time,
        "User-Agent": navigator.userAgent,
        sign: params.sign
    };

    // 公共参数合并其他参数
    data = $.extend(data, params.data);


    // 发送
	$.ajax({
	type: "post",
        url: url + '/bhvLogApi/uploadBhvLog',
        data: data,
        dataType: "json",
        sunccess: function () {

        }
    })
};

/**
 * 第三方数据埋点
 */
var uploadTuijianLog = function(data){
	var domain = document.domain;

    var patt = /\.(test|dev)\./i;
    var dev = domain.search(patt);
    // 根据域名获取发送日志地址
    if(dev > 0)
    {
	    //var url = 'http://192.168.1.158:7201';
	    var url = 'http://192.168.1.130:7201';
    }else
    {
	    var url = 'https://log.mbalib.com';
    }
    return false;



    // 发送
	$.ajax({
	type: "post",
        url: url + '/bhvLogApi/uploadLog2',
        data: data,
        dataType: "json",
        sunccess: function () {

        }
    })
};


/**
 * 日志埋点 - 搜索
 * @param params {ip: string, time: *, sign: *, data: {除公共参数外的其他参数}}
 */
var uploadKeyword = function (plate,content) {
    var domain = document.domain;

    var patt = /\.(test|dev)\./i;
    var dev = domain.search(patt);
    // 根据域名获取发送日志地址
    if(dev > 0)
    {
	    var url = 'http://192.168.1.158:7201';
	    //var url = 'http://192.168.1.130:7201';
    }else
    {
	    var url = 'https://log.mbalib.com';
    }
    return false;

    var data = {
	    'plate':plate,
	    'content':content
    };



    // 发送
	$.ajax({
	type: "post",
        url: url + '/bhvLogApi/uploadKeyword',
        data: data,
        dataType: "json",
        sunccess: function () {

        }
    })
};
